//
//  ScenicDetailHeadView.swift
//  LeTu
//
//  Created by g1 on 2024/1/11.
//

import UIKit

class ScenicDetailHeadView: UIView {

    private(set) var isExpandOrgDetails = false
    private var scenicModel = ScenicModel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
    }
    static func getHeadImgViewHeight() -> CGFloat {
        return 266
    }
    static func getHeadHeight(scenicModel: ScenicModel) -> CGFloat {
        
        var headHeight = 22.0
        
        let nameHeight = 22.0
        headHeight += nameHeight
        
        let levelHeight = 12.0 + 14.0
        headHeight += levelHeight

        let introduceText = LanguageManager.shared.currentLanguage == .en ? scenicModel.englishBrief:scenicModel.brief
        let introduceHeight = 12.0 + min(introduceText.heightWithLimit(width: screenWidth-32, font: kSetPingFangRegular(12)), 34)
        headHeight += introduceHeight
        
        let lineHeight = 12.0+10.0
        headHeight += lineHeight

        return (getHeadImgViewHeight()-16) + headHeight
    }
    
    // MARK: - setData
    func setScenicModel(_ scenicModel: ScenicModel) {
        
        // 设置景区等级
        let scenicLevelText = "\(scenicModel.scenicLevel)A"
        scenicLevelLabel.text = scenicLevelText
        scenicLevelLabel.snp.updateConstraints { make in
            make.width.equalTo(scenicLevelText.widthWithLimit(CGSize(width: CGFloat.greatestFiniteMagnitude, height: 14), font: kSetPingFangRegular(11))+10)
        }
        
        // 讲解数
        audioCountLabel.text = "\(scenicModel.storyNum)"+"处讲解".localized
        
        // 景区介绍
        introduceLabel.text = LanguageManager.shared.currentLanguage == .en ? scenicModel.englishBrief : scenicModel.brief
        introduceLabel.snp.updateConstraints { make in
            make.height.equalTo(introduceLabel.sizeThatFits(CGSize(width: screenWidth-32, height: CGFloat.greatestFiniteMagnitude)))
        }
        
        // 设置景区名称
        nameLabel.text = LanguageManager.shared.currentLanguage == .en ? scenicModel.englishName:scenicModel.name
        
        // 设置景区点赞数
        zanLabel.attributed.text = "\(.image(UIImage(named: scenicModel.hasCollected ? "collect_zan" : "collect_unzan")!, .custom(.offset(CGPoint(x: 0, y: -1)), size: CGSize(width: 14, height: 14)))) \(scenicModel.collections)"
        
        // 设置景区图片
        topImgView.sd_setImage(with: URL.encodedURL(string: scenicModel.picUrl))
        
        // 收听数
        listenCountLabel.attributed.text = "\(.image(UIImage(named: "listeners_count_black_icon")!, .custom(.offset(CGPoint(x: 0, y: -2)), size: CGSize(width: 13, height: 13)))) \(scenicModel.listeners)"+"人已听".localized
        
    }
    
    // MARK: - UIButtonAction
    @objc private func zanLabelGrAction() {
        send(event: ScenicDetailVC.TransferEvent.collectScenic)
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        backView.addSubview(topImgView)
        backView.addSubview(infoBackView)
        infoBackView.addSubview(nameLabel)
        infoBackView.addSubview(zanLabel)
        infoBackView.addSubview(scenicLevelLabel)
        infoBackView.addSubview(audioCountBackView)
        audioCountBackView.addSubview(audioCountImgView)
        audioCountBackView.addSubview(audioCountLabel)
        infoBackView.addSubview(listenCountLabel)
        infoBackView.addSubview(introduceLabel)
        infoBackView.addSubview(lineView)
        
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        infoBackView.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
        }
        zanLabel.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.height.equalTo(22)
            make.top.equalTo(22)
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.lessThanOrEqualTo(zanLabel.snp.left).offset(-10)
            make.height.centerY.equalTo(zanLabel)
        }
        scenicLevelLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.width.equalTo(0)
            make.height.equalTo(14)
            make.top.equalTo(nameLabel.snp.bottom).offset(12)
        }
        audioCountBackView.snp.makeConstraints { make in
            make.left.equalTo(scenicLevelLabel.snp.right).offset(8)
            make.centerY.equalTo(scenicLevelLabel)
            make.height.equalTo(14)
        }
        audioCountImgView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(3)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(11)
        }
        audioCountLabel.snp.makeConstraints { make in
            make.left.equalTo(audioCountImgView.snp.right).offset(2)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-6)
        }
        listenCountLabel.snp.makeConstraints { make in
            make.right.equalTo(zanLabel)
            make.centerY.equalTo(scenicLevelLabel)
            make.height.equalTo(14)
        }
        introduceLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.right.equalTo(zanLabel)
            make.top.equalTo(scenicLevelLabel.snp.bottom).offset(12)
            make.height.equalTo(0)
        }
        lineView.snp.makeConstraints { make in
            make.top.equalTo(introduceLabel.snp.bottom).offset(12)
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(10)
        }
        topImgView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.bottom.equalTo(infoBackView.snp.top).offset(16)
        }

        let zanTapGR = UITapGestureRecognizer(target: self, action: #selector(zanLabelGrAction))
        zanLabel.addGestureRecognizer(zanTapGR)
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        backView.backgroundColor = .white
        return backView
    }()
    private lazy var infoBackView: UIView = {
        let infoBackView = UIView()
        infoBackView.backgroundColor = .white
        infoBackView.layer.cornerRadius = 16
        infoBackView.layer.masksToBounds = true
        return infoBackView
    }()
    private var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.font = kSetPingFangRegular(22)
        return nameLabel
    }()
    private var zanLabel: UILabel = {
        let zanLabel = UILabel()
        zanLabel.font = kSetPingFangRegular(12)
        zanLabel.textAlignment = .right
        return zanLabel
    }()
    private lazy var scenicLevelLabel: UILabel = {
        let scenicLevelLabel = UILabel()
        scenicLevelLabel.textColor = .app_white
        scenicLevelLabel.font = kSetPingFangRegular(11)
        scenicLevelLabel.textAlignment = .center
        scenicLevelLabel.backgroundColor = UIColor.hex(hexString: "#9C4615")
        scenicLevelLabel.layer.cornerRadius = 7
        scenicLevelLabel.layer.masksToBounds = true
        return scenicLevelLabel
    }()
    private lazy var audioCountBackView: UIView = {
        let audioCountBackView = UIView()
        audioCountBackView.backgroundColor = UIColor.hex(hexString: "#E7BA1B")
        audioCountBackView.layer.cornerRadius = 7
        audioCountBackView.layer.masksToBounds = true
        return audioCountBackView
    }()
    private lazy var audioCountImgView: UIImageView = {
        let audioCountImgView = UIImageView()
        audioCountImgView.image = UIImage(named: "play_white_small")
        return audioCountImgView
    }()
    private lazy var audioCountLabel: UILabel = {
        let audioCountLabel = UILabel()
        audioCountLabel.textColor = .app_white
        audioCountLabel.font = kSetPingFangRegular(10)
        return audioCountLabel
    }()
    private lazy var listenCountLabel: UILabel = {
        let listenCountLabel = UILabel()
        listenCountLabel.textColor = UIColor.hex(hexString: "#464B52")
        listenCountLabel.font = kSetPingFangRegular(12)
        listenCountLabel.textAlignment = .right
        return listenCountLabel
    }()
    private var introduceLabel: UILabel = {
        let introduceLabel = UILabel()
        introduceLabel.font = kSetPingFangRegular(12)
        introduceLabel.textColor = UIColor.hex(hexString: "A1A1A1")
        introduceLabel.numberOfLines = 2
        return introduceLabel
    }()
    private lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = .app_viewBackgroundColor
        return lineView
    }()
    
    // -- 背景图
    private var topImgView: UIImageView = {
        let topImgView = UIImageView()
        topImgView.contentMode = .scaleAspectFill
        topImgView.clipsToBounds = true
        return topImgView
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
